package com.example.bbgds.test;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;

/**
 * Created by zjzhu on 2/3/16.
 */
public class VoiceDragLayout extends FrameLayout implements EventController.EventCallback{

    private FragmentManager mFragmentManager;

    private HistoryView mHistoryView;
    private Fragment mHotSearchFragment;
    private Fragment mHelpListFragment;
    private EventController mEventController;

    public VoiceDragLayout(Context context) {
        this(context, null);
    }

    public VoiceDragLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoiceDragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onFinishInflate(){
        super.onFinishInflate();
//        mHistoryView = (HistoryView)findViewById(R.id.bottom);
        mEventController = new EventController(this, this);
//        mEventController.addDropTarget(mHistoryView);
    }

    public void setFragmentManager(FragmentManager manager){
        mFragmentManager = manager;
    }

    private FragmentManager getFragmentManager(){
        return mFragmentManager;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(mEventController.onTouchEvent(ev)){
            return true;
        }
        return true;
    }

    public void onResume(){
        Log.e("zhu","getFragmentManager().getBackStackEntryCount()=" + getFragmentManager().getBackStackEntryCount());
        if(getFragmentManager().getBackStackEntryCount() > 0){
            getFragmentManager().popBackStackImmediate(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    public void onPause(){
//        getFragmentManager().popBackStackImmediate(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(mEventController.onInterceptTouchEvent(ev)){
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }


    @Override
    public void flingUp(){
        if(mHotSearchFragment == null){
            mHotSearchFragment = getHotSearchFragment();
        }
        if(!mHotSearchFragment.isAdded()){
            addTopFragment(mHotSearchFragment);
            return;
        }
        if(mHelpListFragment == null){
            mHelpListFragment = getHelpListFragment();
        }
        if(!mHelpListFragment.isAdded()){
            addTopFragment(mHelpListFragment);
            return;
        }
    }

    @Override
    public void flingDown(){
        if((mHotSearchFragment != null && mHotSearchFragment.isAdded()) ||
                (mHelpListFragment != null && mHelpListFragment.isAdded())){
            mFragmentManager.popBackStack();
            return;
        }
    }

    private void addTopFragment(Fragment fragment){
//        FragmentTransaction transaction = getFragmentManager().beginTransaction();
//        transaction.setCustomAnimations(R.animator.slide_bottom_in_onebox, R.animator.slide_bottom_out_onebox,
//                R.animator.slide_bottom_in_onebox, R.animator.slide_bottom_out_onebox);
//        transaction.add(R.id.test, fragment, String.valueOf(getFragmentManager().getBackStackEntryCount()));
//        transaction.addToBackStack(null);
//        transaction.commit();
    }

    private void addBottomFragment(Fragment fragment){
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.slide_top_in_onebox, R.animator.slide_top_out_onebox,
                R.animator.slide_top_in_onebox, R.animator.slide_top_out_onebox);
        transaction.add(R.id.bottom, fragment, String.valueOf(getFragmentManager().getBackStackEntryCount()));
        transaction.addToBackStack(null);
        transaction.commit();
    }



    public Fragment getHotSearchFragment(){
        return new HotListFragment();
    }

    public Fragment getHelpListFragment(){
        return new HelpFragment();
    }
}
